package com.ruoyi.web.controller.workJob;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.worker.domain.BusinessVolume;
import com.ruoyi.worker.domain.StrategyItem;
import com.ruoyi.worker.domain.StrategyPage;
import com.ruoyi.worker.service.IStrategyConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: StrategyConfigController
 * @Description:策略配置
 * @Author: 刘松山
 * @Date: 2021/10/11 10:59
 */
@RestController
@RequestMapping("/workJobApi/strategyConfig")
public class StrategyConfigController extends BaseController {

    @Autowired
    private IStrategyConfigService iStrategyConfigService;

    @Log(title = "策略查询")
    @GetMapping("/list")
    public AjaxResult list() {
        AjaxResult ajax = AjaxResult.success();

        List<StrategyItem> itemList = iStrategyConfigService.listStrategyItem();
        StrategyPage strategyPage = new StrategyPage();
        List<Map<String,Object>> mapList = new ArrayList<>();
        if(itemList.size() >0){
        strategyPage.setStrategy1(itemList.get(0).getStrategyValue());
        strategyPage.setStrategy2(itemList.get(1).getStrategyValue());
        strategyPage.setStrategy3(itemList.get(2).getStrategyValue());
            if(strategyPage.getStrategy3()==2){
                List<BusinessVolume> list = iStrategyConfigService.ListBusinessVolume();
                strategyPage.setBusinessVolumeValue(list.get(0).getBusinessVolumeValue()+"");
            }else if(strategyPage.getStrategy3()==3){
                List<BusinessVolume> list = iStrategyConfigService.ListBusinessVolume();
                List<Map<String,Object>> titleList = iStrategyConfigService.ListSchemeConfiguration();
                int  i=0;
                Map<String,Object> map = new HashMap<>();
                map.put("name","设置比率%");
                map.put("isEdit",false);
                for(Map<String,Object> m:titleList){
                    i++;

                    int  flag =0;
                    for(BusinessVolume b:list){

                        if(b.getBusinessId()==Long.parseLong(m.get("id")+"")){
                            flag =1;
                            map.put("value"+i,b.getBusinessVolumeValue());
                            break;
                        }
                    }
                    if(flag ==0){
                        map.put("value"+i,"");
                    }

                }
                mapList.add(map);
                strategyPage.setList(mapList);
            }
        }else{
            strategyPage.setStrategy1(1);
            strategyPage.setStrategy2(1);
            strategyPage.setStrategy3(1);
        }

        ajax.put(AjaxResult.DATA_TAG,strategyPage);
         return ajax;
    }

    @Log(title = "查询业务量分档表头")
    @GetMapping("/listTabletitle")
    public AjaxResult listTabletitle() {
        AjaxResult ajax = AjaxResult.success();

        List<Map<String,Object>> itemList = iStrategyConfigService.ListSchemeConfiguration();

        ajax.put(AjaxResult.DATA_TAG,itemList);
        return ajax;
    }

    @Log(title = "策略保存", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @RepeatSubmit
    public AjaxResult add(@Validated @RequestBody StrategyPage strategyPage) {
        strategyPage.setCreateBy(getUsername());
        return toAjax(iStrategyConfigService.addStrategyItem(strategyPage));
    }
}
